package com.share.spock.service

import com.share.spock.model.Address
import com.share.spock.model.Pay
import com.share.spock.model.dto.TestDTO
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Specification
/**
 * @author caifan* @since 2021/9/22
 * description: 
 */
@SpringBootTest
class UserServiceSpec extends Specification {

    @SpringBean
    private AddressService addressService = Mock();

    @SpringBean
    private PayService payService = Mock();

    @Autowired
    private UserService userService;

    def "userService"() {
        given:
        def address = new Address(province: "广东省", city: "广州市", area: "海珠区");
        def pay = new Pay(price: 100.23, totalPrice: 1003.23, orderId: 11334L, userId: 123L);
        addressService.getAdress() >> address
        payService.getPay() >> pay

        when:
        def result = userService.getUser();

        then:
        with(result) {
//            result.userId == 123L
            area == '海珠区'
            totalPrice == 1003.23
        }
    }

    def "user address list"() {
        given:
        def address = addressList();
        def pay = new Pay(price: 100.23, totalPrice: 1003.23, orderId: 11334L, userId: 123L);
        addressService.listAddressByUserId(123L) >> address
        payService.getPay() >> pay

        when:
        def result = userService.getUserById(123L);

        then:
        result == true
        //with(result) {
//            result.userId == 123L
//             addressList.size() == 2
//            totalPrice == 1003.23
        //}
    }

    def "user list01"() {
        given:
        def address = new Address(province: "广东省", city: "广州市", area: "海珠区");
        def pay = new Pay(price: 100.23, totalPrice: 1003.23, orderId: 11334L, userId: 123L);
        addressService.getAddress(123L, "海珠区") >> address
        payService.getPay() >> pay

        when:
        def result = userService.getUserByIdAndArea(123L, "海珠区");

        then:
        with(result) {
//            result.userId == 123L
//             addressList.size() == 2
            totalPrice == 1003.23
        }
    }

    def "user list02"() {
        given:
        def address = new Address(province: "广东省", city: "广州市", area: "海珠区");
        def pay = new Pay(price: 100.23, totalPrice: 1003.23, orderId: 11334L, userId: 123L);
        def dto = new TestDTO();//new TestDTO(city:  "广州", area:  "天河");//Mock(TestDTO)
        dto.setCity("广州")
        dto.setArea("天河")
        dto.setIdList(Collections.singletonList(2L))
        addressService.getAddress(123L, dto) >> address
        payService.getPay() >> pay

        when:
        def result = userService.getUserByIdAndArea(123L, "海珠区");

        then:
        with(result) {
//            result.userId == 123L
//             addressList.size() == 2
            totalPrice == 1003.23
        }
    }

    def addressList() {
        List<Address> addressList = new ArrayList<>();
        Address address1 = new Address();
        address1.setProvince("广东省");
        address1.setCity("广州市");
        address1.setArea("海珠区");
        address1.setUserId(121L);
        Address address2 = new Address();
        address2.setProvince("广东省");
        address2.setCity("广州市");
        address2.setArea("海珠区");
        address2.setUserId(121L);
        addressList.add(address1);
        addressList.add(address2);
        return addressList;
    }
}
